﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinqToPi;
using System.Diagnostics;
using System.Collections;
using System.Reflection;

namespace LinqToPi.Clients.Console.LinqToPiTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string tagname = "sinusoid";
            var startTime = DateTime.Now.AddHours(-1);
            var endTime = DateTime.Now;
            PiServerContext context = new PiServerContext("nlms028.nl.int.atosorigin.com", "piadmin", System.Console.Out);
            var ret = from
                          v
                      in
                          context.Archive.ArchiveData
                      where
                           v.Tag == tagname &&
                           v.Time.Between(startTime, endTime)
                      select v;

            context.Archive.ArchiveData.Select(v => v.Tag == tagname && v.Time.Between(startTime, endTime));
          

            //PiValue val = ret.First();
            //val.Value = -9999;
            //context.Archive.ArchiveData.Update(val);
            //context.SubmitChanges();
          
            Report<PiValue>(ret);

            //Report<PiLogItem>(context.System.MessageLog.Where(c => c.Message.Contains("LinqToPi")));

            //Report<PiLogItem>(context.System.MessageLog.Take(10));

            //var snaps = from s in context.Archive.SnapShots.Take(10)
            //            select s;
            //Report<PiValue>(snaps);

            //var mins = from m in context.Archive.MinValues
            //           where m.Time.Between(DateTime.Now.AddDays(-2), DateTime.Now) &&
            //           m.Tag == "sinusoid"
            //           select m;

            //Report<PiMinBoundryValue>(mins);

            //var maxs = from m in context.Archive.MaxValues
            //           where m.Time.Between(DateTime.Now.AddDays(-2), DateTime.Now) &&
            //           m.Tag == "sinusoid"
            //           select m;

            //Report<PiMaxBoundryValue>(maxs);

            //var totals = from m in context.Archive.TotalValues
            //             where m.Time.Between(DateTime.Now.AddDays(-4), DateTime.Today.AddHours(5)) &&
            //             m.Tag == "sinusoid"
            //             select m;
            //Report<PiTotalValue>(totals);

            //Still has error with single quotes ("'")
            //var calculated = from m in context.Archive.CalculatedValues
            //                 where
            //                    m.Expr == "1+1"
            //                 && m.Time.Between(DateTime.Now.AddDays(-2), DateTime.Now)
            //                 select m;

            //Report<PiCalcValue>(calculated);

            //Interpolated values have no annotations...
            //var interpolated = from i in context.Archive.InterpolatedValues
            //                   where i.Time.Between(DateTime.Now.AddDays(-2), DateTime.Now) &&
            //                   i.Tag == "sinusoid"
            //                   select i;

            //Report<PiValue>(interpolated);

            //var points = from p in context.Point.Points
            //             where p.Tag == "sinusoid"
            //             select p;
            //Report<PiPoint>(points);

            //var point = points.First();
            ////point.Span = 1000;
            //context.Point.Points.Delete(point);

            //Report<PiPoint>(points);
            //context.Point.Points.Insert(new PiPoint() { Tag = "Test_Insert123", PtClassName = "classic", PointTypeX = "float32", Zero = 0, Span = 2 });



            //var points = from p in context.Point.Points
            //             join s in context.Archive.SnapShots
            //                on p.Tag equals s.Tag
            //             where p.Tag == "sinusoid"

            //             select new { Tag = p.Tag, Descriptor = p.Descriptor, SnapShot = s.Value, EngUnits = p.EngUnits };

            //foreach (var p in points)
            //    System.Console.WriteLine(p.Tag + " " + p.Descriptor + " " + p.SnapShot + " " + p.EngUnits);

            //var users = from u in context.User.Users
            //            join m in context.User.UserMemberships
            //            on u.Name equals m.UserName
            //            select new { Name = u.Name, Group = m.GroupName };

            //foreach (var u in users)
            //    System.Console.WriteLine(u.Name + " " + u.Group);

            //var versions = from v in context.System.ProductVersions
            //               select v;

            //foreach (var v in versions)
            //    System.Console.WriteLine(v.Item + " " + v.Version + " " + v.Descriptor);

            //context.User.Users.Insert(new PiUser() { Name = "Michael", Description = "Test Account" });
            //foreach (var u in context.User.Users)
            //    System.Console.WriteLine(u.Name);

            //var values = (from v in context.Archive.ArchiveData
            //              where v.Tag.StartsWith("sin")
            //                    && v.Time > DateTime.Now.AddHours(-1)
            //                    && v.Status == 0
            //              select v);

            //foreach (var v in values)
            //    System.Console.WriteLine(v.Tag + " " + v.Value);

            //var vo = values.First();
            //vo.Value = 100;
            //context.Archive.ArchiveData.Update(vo);
            //PiValue val = values.First();
            //val.Value = -9999;
            //context.Archive.ArchiveData.Update(val, v => v.Tag == v.Tag);


            //var values2 = from v in context.Archive.AverageValues
            //              where v.Tag.StartsWith("SINUS") && v.Time.Between(DateTime.Today, DateTime.Now)
            //              select v;

            //foreach (var v in values2)
            //    System.Console.WriteLine(v.Tag + " " + v.Value + " " + v.TimeStep);


           

            //var messages = (from m in context.System.MessageLog
            //                select m).Take(20);

            //foreach (var m in messages)
            //    System.Console.WriteLine(m.Id + " " + m.Source + " " + m.Message);

            //context.System.MessageLog.Insert(new PiLogItem() { Message = "Hello Pi, from LinqToPi" });
          


            System.Console.ReadLine();
        }

        private static void Report<T>(IEnumerable p)
        {
            Type t = typeof(T);
            StringBuilder b = new StringBuilder();
            b.AppendLine("".PadRight(10, '-'));
            foreach (object o in p)
            {
                foreach (PropertyInfo pinfo in t.GetProperties())
                {
                    b.AppendLine(pinfo.Name.PadRight(20) + ": " + pinfo.GetValue(o, null));
                }
                b.AppendLine();
            }
            b.AppendLine("".PadLeft(10, '-'));
            System.Console.WriteLine(b);
        }
    }
}
